package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.comic.common.threadlocal.SessionLoad;
import com.nifostasky.comic.entitys.CoHistoryRecord;
import com.nifostasky.comic.entitys.CoOtherPicture;
import com.nifostasky.comic.entitys.CoSpiderInfo;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.mapper.CoHistoryRecordMapper;
import com.nifostasky.comic.mapper.CoSpiderInfoMapper;
import com.nifostasky.comic.query.CoSpiderInfoQuery;
import com.nifostasky.comic.service.CoDetailedInfoService;
import com.nifostasky.comic.service.CoHistoryRecordService;
import com.nifostasky.comic.service.CoSpiderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 爬取网页python文件信息
 * (CoSpiderInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-02-13 15:00:45
 */
@Service("coHistoryRecordService")
public class CoHistoryRecordServiceImpl extends ServiceImpl<CoHistoryRecordMapper, CoHistoryRecord> implements CoHistoryRecordService {

    @Override
    public Map<String, Object> queryHistoryRecordPage(Integer pageIndex, Integer pageSize,Long userId) {
        //查询
        if (Objects.isNull(pageIndex)) pageIndex = 1;
        if (Objects.isNull(pageSize)) pageSize = 10;
        Map<String,Object> map = new HashMap<>();
        Page<CoHistoryRecord> page = this.page(new Page<CoHistoryRecord>(pageIndex, pageSize), new QueryWrapper<CoHistoryRecord>().lambda()
                .eq(CoHistoryRecord::getUserId,userId)
                .orderByDesc(CoHistoryRecord::getCreateDate)
        );
        map.put("total",page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }

    @Override
    public Integer deleteDateRange7() {
        //删除7天之前的历史记录
        LocalDate localDate = new LocalDate();
        LocalDate localDate7 = localDate.plusDays(-7);
        this.remove(new QueryWrapper<CoHistoryRecord>().lambda()
                .lt(CoHistoryRecord::getCreateDate,localDate7)
        );
        return 1;
    }

    @Override
    public Integer saveHistoryRecord(CoHistoryRecord history) {
        history.setUserName(SessionLoad.getLocalData().getUserName());
        history.setUserId(SessionLoad.getLocalData().getUserId());
        this.save(history);
        return 1;
    }

}